package Servlet;

import beans.Complex;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet(urlPatterns = {"/caculate"})
public class HttpServlet extends javax.servlet.http.HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        double r1=Double.parseDouble(request.getParameter("realPart1"));
        double i1=Double.parseDouble(request.getParameter("imagePart1"));
        String oper=request.getParameter("operation");
        double r2=Double.parseDouble(request.getParameter("realPart2"));
        double i2=Double.parseDouble(request.getParameter("imagePart2"));
        Complex c1=new Complex(r1,i1);
        Complex c2=new Complex(r2,i2);
        String result="";
        if("+".equals(oper)) result=c1.add(c2).info();
        else if("-".equals(oper)) result=c1.sub(c2).info();
        else if("*".equals(oper)) result=c1.mul(c2).info();
        else result=c1.div(c2).info();
        request.setAttribute("outMessage",result);
        request.getRequestDispatcher("/1output.jsp").forward(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }
}
